using Google.Protobuf;

namespace Shioho.Net
{
    public abstract class AProtoMsgPaser<T> : IProtoMsgPaser
        where T : IMessage<T>
    {
        public int ProtoId => _protoId;

        protected abstract MessageParser<T> _msgPaser { get; }
        protected abstract int _protoId { get; }

        public IMessage Decode(byte[] buffer)
        {
            return _msgPaser.ParseFrom(buffer);
        }

        public byte[] Encode(IMessage data)
        {
            return data.ToByteArray();
        }
    }

    public abstract class AProtoResponseMsgPaser<T> : AProtoMsgPaser<T>, IProtoResponseMsgPaser
        where T : IMessage<T>
    {
        public long GetCode(IMessage data)
        {
            var propertyInfo = data.GetType().GetProperty("Code");
            long code = propertyInfo == null ? (long)ProtoMsgCodeDefine.Success : (long)propertyInfo.GetValue(data);
            return code;
        }

        public string GetInfo(IMessage data)
        {
            var propertyInfo = data.GetType().GetProperty("Info");
            string info = propertyInfo == null ? "" : (string)propertyInfo.GetValue(data);
            return info;
        }
    }
}
